package com.newgoldcurrency.utils.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.newgoldcurrency.App;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Operator {
    private final SQLiteDatabase db;
    public Operator(Context context){
        db=new SqliteHelper(context).getReadableDatabase();
    }
    public JSONArray addUnreads(boolean back,JsonArray ja,String fromId,String toId){
        db.beginTransaction();
        for (int i=0;i<ja.size();i++){
            JsonObject jo = JsonParser.parseString(ja.get(i).getAsString().replace("\\\\", "\\")).getAsJsonObject();
            ContentValues values = new ContentValues();
            values.put("timestamp", jo.get("create_time").getAsString());
            values.put("content", jo.get("msg").getAsString());
            values.put("fromId",fromId);
            values.put("toId", toId);
            values.put("ownerId", App.userBean.id);
            db.insert("CHAT", null, values);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        if (back){
            return getAsJson(db.rawQuery("SELECT  * FROM CHAT WHERE (fromId = '"+fromId+"' AND toId = '"+toId+"' AND ownerId = '"+App.userBean.id+"' ) OR ( fromId = '"+toId+"' AND toId = '"+fromId+"' AND ownerId = '"+App.userBean.id+"')",null));
        }else {
            return null;
        }
    }
    public void addUnread(String content,String fromId,String toId,String timestamp){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("timestamp",timestamp);
        values.put("content", content);
        values.put("fromId",fromId);
        values.put("toId", toId);
        values.put("ownerId", App.userBean.id);
        db.insert("CHAT", null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }
    private JSONArray getAsJson(Cursor cursor) {
        JSONArray resultSet = new JSONArray();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();
            for( int i=0 ;  i< totalColumn ; i++ ) {
                if( cursor.getColumnName(i) != null) {
                    try {
                        if( cursor.getString(i) != null ) rowObject.put(cursor.getColumnName(i), cursor.getString(i) );
                        else rowObject.put( cursor.getColumnName(i) ,  "" );
                    } catch( Exception ignored) { }
                }
            }
            resultSet.put(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
        return resultSet;
    }
    public void delete(String myId, String otherId) {
        db.execSQL("DELETE FROM CHAT WHERE (fromId = '"+myId+"' AND toId = '"+otherId+"') OR (fromId = '"+otherId+"' AND toId = '"+myId+"')");
    }
    public boolean getMailVerify(String mail,String verify){
        JSONArray res = getAsJson(db.rawQuery("SELECT * FROM USER WHERE ( mail = '" + mail + "' )", null));
        if (verify!=null){//input
            if (res.length()>0){
                ContentValues values = new ContentValues();
                values.put("verify", verify);
                db.update("USER",values, "mail=?" ,new String[]{mail});
            }else{
                db.beginTransaction();
                ContentValues values = new ContentValues();
                values.put("mail", mail);
                values.put("verify", verify);
                db.insert("USER", null, values);
                db.setTransactionSuccessful();
                db.endTransaction();
            }
            return !verify.equals("0");
        }else {
            if (res.length()>0){
                try {
                    return !res.getJSONObject(0).getString("verify").equals("0");
                } catch (JSONException e) {
                    return false;
                }
            }else{
                db.beginTransaction();
                ContentValues values = new ContentValues();
                values.put("mail", mail);
                values.put("verify", "0");
                db.insert("USER", null, values);
                db.setTransactionSuccessful();
                db.endTransaction();
                return false;
            }


        }


    }
}
